﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Krist_em_up.Characters;

namespace Krist_em_up.Interface.Gui
{
    public class PlayerHealth
    {
        Texture2D texture;
        Kristemup game;
        SpriteFont font;
        Effect desaturate;

        public PlayerHealth(Kristemup g)
        {
            game = g;
            texture = game.Content.Load<Texture2D>("heart");
            desaturate = game.Content.Load<Effect>("desaturate");
            font = game.Content.Load<SpriteFont>("Fonts/Gamefont");
        }

        public void Draw(SpriteBatch spritebatch,float percent)
        {
            spritebatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
            Rectangle r1 = new Rectangle(game.game.GraphicsDevice.Viewport.Width / 2 - texture.Width / 2, 0, texture.Width, texture.Height);
            desaturate.Begin();
            desaturate.CurrentTechnique.Passes[0].Begin();

            spritebatch.Draw(texture, r1, Color.White);

            spritebatch.End();
            desaturate.End();
            desaturate.CurrentTechnique.Passes[0].End();

            spritebatch.Begin();
            
            int decalage = (int)((1f - percent) * (float)texture.Height);
            Rectangle r2 = new Rectangle(game.game.GraphicsDevice.Viewport.Width / 2 - texture.Width / 2, decalage, texture.Width, texture.Height - decalage);
            spritebatch.Draw(texture, r2, new Rectangle(0, decalage, texture.Width, texture.Height - decalage), Color.White);
            percent = (int)Math.Truncate(percent*100f);
            Vector2 size = font.MeasureString(percent.ToString());
            spritebatch.DrawString(font, percent.ToString(), new Vector2(game.game.GraphicsDevice.Viewport.Width / 2 - texture.Width / 2 + (texture.Width - font.MeasureString(percent.ToString()).X) / 2, r1.Y + texture.Height /2 - size.Y /2), Color.White);
            spritebatch.End();

            
            
        }
    }
}
